var shu = [2,4,8,16,32,64,128,256,512,1024,2048,4096];
var zan = new Array();
var ulId = new Array();
var jishi,fenshu;
var sum = new Number();
window.onload = function(){
    var j = 1;
    var startX,endX,startY,endY;
    var u = document.getElementById("u1");
    jishi = document.getElementById("jishi");
    fenshu = document.getElementById("fenshu");
    for(var i = 0;i <= 15;i++,j++){
        ulId[i] = document.getElementById("l" + j);
    }      
    function cons(){
        console.log(2);
        if(startX > endX && (endY - startY) < 100 && (startY - endY) < 100){
            console.log("左");
            fx(1);
        }else if(startX < endX && (endY - startY) < 100 && (startY - endY) < 100){
            console.log("右");
            fx(2);
        }else if(startY > endY){
            console.log("上");
            fx(3);
        }else if(startY < endY){
            console.log("下");
            fx(4);
        }
    }

    u.addEventListener('touchstart' , function(event){
        var touch = event.changedTouches;
        startX = touch[0].clientX;
        startY = touch[0].clientY;
    });
    u.addEventListener('touchend', function(){
        var touch = event.changedTouches;
        endX = touch[0].clientX;
        endY = touch[0].clientY;
        cons();
    });
}
function yanse(){
    for(var j in ulId){
        if(ulId[j].innerHTML == 2) ulId[j].style.background = '#02eef6';
        if(ulId[j].innerHTML == 4) ulId[j].style.background = "#16c9cf"
        if(ulId[j].innerHTML == 8) ulId[j].style.background = "#0eadb3";
        if(ulId[j].innerHTML == 16) ulId[j].style.background = "#1c969a";

        if(ulId[j].innerHTML == 32) ulId[j].style.background = "#ea10c5";
        if(ulId[j].innerHTML == 64) ulId[j].style.background = "#cc19ae";
        if(ulId[j].innerHTML == 128) ulId[j].style.background = "#a91790";
        if(ulId[j].innerHTML == 256) ulId[j].style.background = "#97077f";

        if(ulId[j].innerHTML == 512) ulId[j].style.background = "#6def09";
        if(ulId[j].innerHTML == 1024) ulId[j].style.background = "#75d42c";
        if(ulId[j].innerHTML == 2048) ulId[j].style.background = "#5dad21";
        if(ulId[j].innerHTML == 4096) ulId[j].style.background = "#448e0c";

        if(ulId[j].innerHTML == "") ulId[j].style.background = "#e7e6e6";
    }
}
function panduan(){
    var cun = new Array();
    var a = 0;
    for(var i in ulId){
        if(ulId[i].innerHTML == ""){
            cun[a] = i;
            a++;
        }
    }
    var pand = true;
    var sui = Math.floor(Math.random() * cun.length);
    console.log(cun);
    console.log(sui);
    produce();
    if(cun.length != 0){
        ulId[cun[sui]].innerHTML = produce();
    }else{
        if( end(0,1,2,3) || end(4,5,6,7) || end(8,9,10,11) || end(12,13,14,15) ||
            end(0,4,8,12) || end(1,5,9,13) || end(2,6,10,14) || end(3,7,11,15)){
                console.log("可以继续玩");
            }else{
                clearInterval(int);
                console.log("结束");
            }
        
    }
    
    
    yanse();
}

function produce(){
    var suiji = Math.floor(Math.random() * 100);
    if(suiji % 2 == 0){
        return 2;
    }else{
        return 4;
    }
}

function fx(num){
    for(var h in ulId){
        zan[h] = ulId[h].innerHTML;
    }
        for(var k in shu){
            if(num == 1){//左
                yd(shu[k],0,1,2,3);
                yd(shu[k],4,5,6,7);
                yd(shu[k],8,9,10,11);
                yd(shu[k],12,13,14,15);
            }else if(num == 2){//右
                yd(shu[k],3,2,1,0);
                yd(shu[k],7,6,5,4);
                yd(shu[k],11,10,9,8);
                yd(shu[k],15,14,13,12);
            }else if(num == 3){//上
                yd(shu[k],0,4,8,12);
                yd(shu[k],1,5,9,13);
                yd(shu[k],2,6,10,14);
                yd(shu[k],3,7,11,15);
            }else if(num == 4){//下
                yd(shu[k],12,8,4,0);
                yd(shu[k],13,9,5,1);
                yd(shu[k],14,10,6,2);
                yd(shu[k],15,11,7,3);
            }
        }
        yanse();
        panduan();
        fenshu.innerHTML = "当前分数 : " + sum;   
}

function yd(num,li1,li2,li3,li4){
   if(ulId[li1].innerHTML == num){
       if(ulId[li2].innerHTML == num){
           ulId[li1].innerHTML = num * 2;
           sum += num * 2; 
           if(ulId[li3].innerHTML == ""){
               ulId[li2].innerHTML = ulId[li4].innerHTML;
           }else{
                ulId[li2].innerHTML = ulId[li3].innerHTML;
                ulId[li3].innerHTML = ulId[li4].innerHTML;
           }
           ulId[li4].innerHTML = "";
           if(ulId[li2].innerHTML == ulId[li3].innerHTML && ulId[li2].innerHTML != "" && ulId[li3].innerHTML != ""){
               ulId[li2].innerHTML = Number(ulId[li2].innerHTML) * 2;
               sum += Number(ulId[li2].innerHTML) * 2; 
               ulId[li3].innerHTML = ulId[li4].innerHTML = "";
           }
       }else if(ulId[li2].innerHTML == ""){
           if(ulId[li3].innerHTML == num){
                ulId[li1].innerHTML = num * 2;
                sum += num * 2; 
                ulId[li2].innerHTML = ulId[li4].innerHTML;
                ulId[li3].innerHTML = ulId[li4].innerHTML = "";
           }else if(ulId[li3].innerHTML == ""){
               if(ulId[li4].innerHTML == num){
                   ulId[li1].innerHTML = num * 2;
                   sum += num * 2; 
                   ulId[li4].innerHTML = "";
               }else if(ulId[li4].innerHTML != num){
                   ulId[li2].innerHTML = ulId[li4].innerHTML;
                   ulId[li4].innerHTML = "";
               }
           }else if(ulId[li3].innerHTML != num){
               ulId[li2].innerHTML = ulId[li3].innerHTML;
               ulId[li3].innerHTML = "";
           }
       }
   }
   if(ulId[li2].innerHTML == num){
       if(ulId[li1].innerHTML == ""){
           if(ulId[li3].innerHTML == num){
               ulId[li1].innerHTML = num * 2;
               sum += num * 2; 
               ulId[li2].innerHTML = ulId[li4].innerHTML;
               ulId[li3].innerHTML = ulId[li4].innerHTML = "";
           }else if(ulId[li3].innerHTML == ""){
               if(ulId[li4].innerHTML == num){
                   ulId[li1].innerHTML = num * 2;
                   sum += num * 2; 
                   ulId[li2].innerHTML = ulId[li4].innerHTML = "";
               }else if(ulId[li4].innerHTML == "" || ulId[li4].innerHTML != num){
                   ulId[li1].innerHTML = ulId[li2].innerHTML;
                   ulId[li2].innerHTML = ulId[li4].innerHTML;
                   ulId[li4].innerHTML = "";
               }
           }else if(ulId[li3].innerHTML != ulId[li4].innerHTML){
               ulId[li1].innerHTML = ulId[li2].innerHTML;
               ulId[li2].innerHTML = ulId[li3].innerHTML;
               ulId[li3].innerHTML = ulId[li4].innerHTML;
               ulId[li4].innerHTML = ""
           }
       }else if(ulId[li1].innerHTML != num){
           if(ulId[li3].innerHTML == num){
               ulId[li2].innerHTML = num * 2;
               sum += num * 2; 
               ulId[li3].innerHTML = ulId[li4].innerHTML;
               ulId[li4].innerHTML = "";
           }else if(ulId[li3].innerHTML == ""){
               if(ulId[li4].innerHTML == num){
                   ulId[li2].innerHTML = num * 2;
                   sum += num * 2; 
               }else if(ulId[li4].innerHTML != num){
                   ulId[li3].innerHTML = ulId[li4].innerHTML;
               }
               ulId[li4].innerHTML = "";
           }
       }
   }
   if(ulId[li3].innerHTML == num){
       if(ulId[li1].innerHTML == "" && ulId[li2].innerHTML == ""){
           if(ulId[li4].innerHTML == num){
               ulId[li1].innerHTML = num * 2;
               sum += num * 2; 
               ulId[li3].innerHTML = ulId[li4].innerHTML = "";
           }else if(ulId[li4].innerHTML != num || ulId[li4].innerHTML == ""){
               ulId[li1].innerHTML = ulId[li3].innerHTML;
               ulId[li2].innerHTML = ulId[li4].innerHTML;
               ulId[li3].innerHTML = ulId[li4].innerHTML = "";
           }
       }else if(ulId[li1].innerHTML != ulId[li2].innerHTML){
           if(ulId[li4].innerHTML == num){
               ulId[li3].innerHTML = num * 2;
               sum += num * 2; 
               ulId[li4].innerHTML = "";
           }else if(ulId[li4].innerHTML != num && ulId[li2].innerHTML == ""){
               ulId[li2].innerHTML = ulId[li3].innerHTML;
               ulId[li3].innerHTML = ulId[li4].innerHTML;
               ulId[li4].innerHTML = "";
           }
       }
   }
   if(ulId[li4].innerHTML == num){
       if(ulId[li1].innerHTML == "" && ulId[li2].innerHTML == "" && ulId[li3].innerHTML == ""){
           ulId[li1].innerHTML = ulId[li4].innerHTML;
           ulId[li4].innerHTML = "";
       }
   }
}

function end(li1,li2,li3,li4){
    if(ulId[li1].innerHTML == ulId[li2].innerHTML ||
        ulId[li2].innerHTML == ulId[li3].innerHTML ||
        ulId[li3].innerHTML == ulId[li4].innerHTML){
            return true;
        }else{
            return false;
        }
}

function start(){
    clearInterval(int);
    int = miao = fen = sum = w = 0;
    zanc = zan = [];
    console.log("和"+sum);
    fenshu.innerHTML = "当前分数 : " + sum;
    jishi.innerHTML = fen + "分" + miao + "秒";
    for(var n in ulId){
        ulId[n].innerHTML = "";
    }
    panduan();
    panduan();
    startTimer();
    
}
var miao = 0, fen = 0,int = 0;
function startTimer(){
    int = setInterval(function(){
        miao++;
        if(miao >= 60){
            fen += 1;
            miao = 0;
        }
        jishi.innerHTML = fen + "分" + miao + "秒";
    },1000);
}

function back(){
        for(var k in zan){
            ulId[k].innerHTML = zan[k];
        }
    yanse();
}